package info.motteke.kurumi;

import info.motteke.kurumi.swt.SwtGuiFactory;
import info.motteke.mixivoice4j.Entry;
import info.motteke.mixivoice4j.MixiVoice;
import info.motteke.mixivoice4j.MixiVoiceBuilder;
import info.motteke.mixivoice4j.Voice;
import info.motteke.util.MixiVoiceProperties;

import java.io.InputStream;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

public class GuiFacade implements VoiceSelectedListener, VoiceInputListener, ImageOpenListener, ImageHolder {

    private final GuiFactory factory;

    private final MixiVoice mixi;

    public GuiFacade(GuiFactory factory, MixiVoice mixi) {
        this.factory = factory;
        this.mixi = mixi;

        mixi.addVoiceCreateListener(new VoiceAddListener(factory.getVoiceTable()));

        factory.getVoiceInput().addVoiceInputListener(this);
        factory.getVoiceTable().addVoiceSelectedListener(this);
        factory.getImageCache().setImageHolder(this);
    }

    public InputStream getImage(String url) {
        return mixi.getImage(url);
    }

    public void start() {
        Timer timer = new Timer("mixi watch");

        timer.scheduleAtFixedRate(new MixiVoiceObserver(mixi), 10, TimeUnit.MINUTES.toMillis(1));

        factory.getWindow().start();
        timer.cancel();

        factory.close();
        MixiVoiceProperties.getInstance().store();
    }

    public void onSend() {
        VoiceInput input = factory.getVoiceInput();

        if (input.isComment()) {
            comment(input);
        } else {
            post(input);
        }

        input.clear();
    }

    private void post(VoiceInput input) {
        String body = input.getInputText();
        if (input.hasImage()) {
            InputStream is = input.getImage();
            String filename = input.getFileName();

            mixi.post(body, is, filename);
        } else {
            mixi.post(body);
        }
    }

    private void comment(VoiceInput input) {
        String body = input.getInputText();
        Entry entry = factory.getVoiceDetail().getDisplayingVoice().getParent();
        mixi.comment(entry, body);
    }

    public void onTableVoiceSelected(Voice voice) {
        factory.getVoiceDetail().showVoice(voice);

        /*
        VoiceRelation rel = factory.getVoiceRelation();
        rel.clear();

        Entry entry = voice.getParent();
        VoiceBlock block;

        block = factory.createVoiceBlock(entry);
        block.addVoiceSelectedListener(this);

        rel.addVoiceBlock(block);

        for (Voice v : entry.getComments()) {
            block = factory.createVoiceBlock(v);
            block.addVoiceSelectedListener(this);
            rel.addVoiceBlock(block);
        }

        rel.reflesh();

        for(Control c : ((SwtVoiceRelation)rel).getBase().getChildren()){
            System.out.println(c.getBounds());
        }
        */
    }

    public void onRelationVoiceSelected(Voice voice) {
        factory.getVoiceDetail().showVoice(voice);
    }

    @Override
    public void onImageDisplay(Entry entry) {
        String title = entry.getBody(new SimpleStringVisitor());
        ImageWindow window = factory.createImageWindow(title, entry.getImageUrl());
        window.start();
    }

    public static void main(String[] args) {
        MixiVoiceProperties p = MixiVoiceProperties.getInstance();

        MixiVoice mixi = MixiVoiceBuilder.build();
        mixi.login(p.getUserId(), p.getPassWord());

        GuiFactory factory = new SwtGuiFactory();

        GuiFacade facade = new GuiFacade(factory, mixi);
        facade.start();
        mixi.close();
        factory.close();
    }
}
